package com.jinke.uang.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.jinke.uang.R;
import com.jinke.uang.utils.PXUtils;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class UpdateVersionDialog extends Dialog implements View.OnClickListener {

    private View rootView;
    private TextView tv_tile;
    private TextView tv_content;
    private SuperTextView confirm_button;
    private NormalClickListener mNormalClickListener;
    private ProgressBar progressBar;

    public UpdateVersionDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public UpdateVersionDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected UpdateVersionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_update_version_dialog, null);
        tv_tile = rootView.findViewById(R.id.tv_tile);
        tv_content = rootView.findViewById(R.id.tv_content);
        confirm_button = rootView.findViewById(R.id.confirm_button);
        progressBar = rootView.findViewById(R.id.progressBar);
        setContentView(rootView);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setLayout(PXUtils.dp2px(getContext(), 260), WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏

        confirm_button.setOnClickListener(this);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }


    public UpdateVersionDialog setTitle(String title) {
        if (tv_tile != null) {
            tv_tile.setText(title);
        }
        return this;
    }


    public UpdateVersionDialog setContent(String contentStr) {
        if (tv_content != null) {
            tv_content.setText(contentStr);
        }
        return this;
    }


    public UpdateVersionDialog updateProgress(int progress) {
        if (progressBar != null) {
            progressBar.setProgress(progress);
        }
        return this;
    }

    public UpdateVersionDialog showProgress() {
        if (progressBar != null) {
            if (progressBar.getVisibility() != View.VISIBLE) {

                progressBar.setVisibility(View.VISIBLE);
            }
        }
        return this;
    }


    public UpdateVersionDialog hideProgress() {

        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_button:
                if (mNormalClickListener != null) {
                    mNormalClickListener.onConfirmClick();
                }
                break;
        }
    }


    public UpdateVersionDialog setNormalClickListener(NormalClickListener listener) {
        mNormalClickListener = listener;
        return this;
    }

    public UpdateVersionDialog setBtnText(String btnText) {
        this.confirm_button.setText(btnText);
        return this;
    }


    public SuperTextView getBtn() {
        return confirm_button;
    }

    public UpdateVersionDialog setBtnBg(@ColorRes int btnColor) {
        this.confirm_button.setDrawable(btnColor);
        this.confirm_button.setCorner(PXUtils.dp2px(getContext(), 23));
        return this;
    }

    public interface NormalClickListener {
        void onConfirmClick();

    }
}
